<?php
require_once(sfConfig::get('sf_plugins_dir').'/sfDoctrineApplyPlugin/modules/sfApply/lib/BasesfApplyActions.class.php');
/**
 * registration actions.
 *
 * @package    SFWebPageT1C
 * @subpackage registration
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class registrationActions extends BasesfApplyActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request){
//        parent::executeSettings($request);
  }
   public function executeApply(sfRequest $request){
    $this->form = $this->newForm('sfApplyApplyForm');
    if ($request->isMethod('post')){
      $this->form->bind($request->getParameter('sfApplyApply'));
      if ($this->form->isValid()){
        $guid = "n" . self::createGuid();
        $this->form->setValidate($guid);
        $this->form->save();
        try{
          $profile = $this->form->getObject();

          $this->mail(array('subject' => sfConfig::get('app_sfApplyPlugin_apply_subject',
              sfContext::getInstance()->getI18N()->__("Please verify your account on %1%", array('%1%' => $this->getRequest()->getHost()))),
            'username' => $profile->getUserName(),
            'email' => $profile->getEmail(),
            'parameters' => array('username' => $profile->getUserName(), 'validate' => $profile->getValidate()),
            'text' => 'registration/sendValidateNewText',
            'html' => 'registration/sendValidateNew'));
          return 'After';
        }catch (Exception $e){
//            echo $e;
//          $mailer->disconnect();
          $profile = $this->form->getObject();
          $user = $profile->getUser();
          $user->delete();
            // You could re-throw $e here if you want to
          // make it available for debugging purposes
          return 'MailerError';
        }
      }
    }
  }

  protected function mail($options) {
    $required = array('subject', 'parameters', 'email', 'username', 'html', 'text');
    foreach ($required as $option)
    {
      if (!isset($options[$option]))
      {
        throw new sfException("Required option $option not supplied to sfApply::mail");
      }
    }
    $message = $this->getMailer()->compose();
    $message->setSubject($options['subject']);

    
    // Render message parts
    $message->setBody($this->getPartial($options['html'], $options['parameters']), 'text/html');
    $message->addPart($this->getPartial($options['text'], $options['parameters']), 'text/plain');
    $address = $this->getFromAddress();
    $message->setFrom(array($address['email'] => $address['fullname']));
    $message->setTo(array($options['email'] => $options['username']));
    $this->getMailer()->send($message);
  }

}
